package com.youlai.system.controller.device;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.system.common.result.PageResult;
import com.youlai.system.pojo.entity.device.DeviceLogEntity;
import com.youlai.system.pojo.query.DeviceLgoPageQuery;
import com.youlai.system.pojo.query.DevicePageQuery;
import com.youlai.system.pojo.vo.device.DeviceLogPageVO;
import com.youlai.system.service.impl.device.DeviceLogService;
import com.youlai.system.service.impl.device.DeviceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Date;
import java.util.stream.Collectors;

@Tag(name = "10.设备日志")
@RestController
@RequestMapping("/api/v1/device/log")
@RequiredArgsConstructor
public class DeviceLogController {


    private final DeviceService deviceService;
    private final DeviceLogService deviceLogService;
    @Operation(summary = "设备日志分页列表", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/page")
    public PageResult<DeviceLogPageVO> page(
            DeviceLgoPageQuery queryParams
    ) {
        LambdaQueryWrapper<DeviceLogEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(queryParams.getDeviceId() != null) {
            lambdaQueryWrapper.eq(DeviceLogEntity::getDeviceId, queryParams.getDeviceId());
        }
        lambdaQueryWrapper.notIn(DeviceLogEntity::getDeviceId, 0);
        if(queryParams.getType() != null) {
            lambdaQueryWrapper.eq(DeviceLogEntity::getType, queryParams.getType());
        }
        if( null != queryParams.getStartTime()) {
            lambdaQueryWrapper.gt(DeviceLogEntity::getCreateTime, new Date(queryParams.getStartTime()));
        }
        if(null != queryParams.getEndTime()) {
            lambdaQueryWrapper.lt(DeviceLogEntity::getCreateTime, new Date(queryParams.getEndTime()));
        }
        if(queryParams.getType() != null) {
            lambdaQueryWrapper.eq(DeviceLogEntity::getType, queryParams.getType());
        }
        lambdaQueryWrapper.orderByDesc(DeviceLogEntity::getId);
        Page<DeviceLogEntity> page = new Page(queryParams.getPageNum(), queryParams.getPageSize());
        page = deviceLogService.page(page, lambdaQueryWrapper);


        IPage<DeviceLogPageVO> logPageVOIPage = new Page<>();
        logPageVOIPage.setPages(page.getPages());
        logPageVOIPage.setTotal(page.getTotal());

        logPageVOIPage.setRecords(page.getRecords()

                .stream().map(
                        deviceLog -> {
                            DeviceLogPageVO deviceLogPageVO = new DeviceLogPageVO();
                            deviceLogPageVO.setDeviceName(deviceService.getById(deviceLog.getDeviceId()).getName());
                            if(new Integer(1).equals(deviceLog.getType())){
                                deviceLogPageVO.setType("上线");
                            }
                            if(new Integer(2).equals(deviceLog.getType())){
                                deviceLogPageVO.setType("下线");
                            }
                            deviceLogPageVO.setMemo(deviceLog.getMemo());
                            deviceLogPageVO.setTime(new DateTime(deviceLog.getCreateTime().getTime())
                                    .toString("yyyy-MM-dd HH:mm:ss"));
                            return deviceLogPageVO;
                        }
                ).collect(Collectors.toList()));

        return PageResult.success(logPageVOIPage);
    }

    public static void main(String[] args) {
        System.out.println();
    }
}
